package com.tianyadu.mmall.core.authentication;

import com.tianyadu.mmall.core.conf.SecurityConstantsConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

/**
 * 抽象通道安全配置
 *
 * @author xueyj
 * @date 2019-11-14 23:23
 */
public class AbstractChannelSecurityConfig extends WebSecurityConfigurerAdapter {

    /**
     * 认证成功处理器
     */
    @Autowired
    protected AuthenticationSuccessHandler authSuccessHandler;

    /**
     * 认证失败处理器
     */
    @Autowired
    protected AuthenticationFailureHandler authFailureHandler;

    /**
     * 配置用户名密码登陆认证信息
     * <p>
     * .loginPage("/authentication/require") 自定义登录页面处理URL
     * .loginProcessingUrl("/authentication/form")  调用usernamePassword过滤器
     * .successHandler(authSuccessHandler) 使用自定义授权认证成功处理器
     * .failureHandler(authFailureHandler) 使用自定义授权认证失败失败处理器
     *
     * @param http HttpSecurity
     * @author xueyj
     * @date 2019/11/14-23:24
     */
    protected void applyPasswordAuthenticationConfig(HttpSecurity http) throws Exception {
        http.formLogin()
                .loginPage(SecurityConstantsConfig.DEFAULT_UNAUTHENTICATION_URL)
                .loginProcessingUrl(SecurityConstantsConfig.DEFAULT_LOGIN_PROCESSING_URL_FORM)
                .successHandler(authSuccessHandler)
                .failureHandler(authFailureHandler);
    }

}